package web;

import java.io.IOException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Crawler implements Runnable
{

	private final List<String> urls;
	private CrawlerRegistration registration;

	public Crawler(List<String> urls, CrawlerRegistration registration)
	{
		this.urls = urls;
		this.registration = registration;
	}

	@Override
	public void run()
	{
		try
		{

			Document doc = null;
			Elements divs = null;
			int pagesOfThisSection = 50;// = 10;
			while (!urls.isEmpty())
			{
				String currentUrl = urls.remove(0);
				pagesOfThisSection = 200;
				
				while (pagesOfThisSection-- > 0)
				{
					doc = Jsoup.connect(currentUrl).timeout(60 * 1000).get();
					divs = doc.select("div.grid-view");
					Element div = null;
					if (divs.size() > 1)
						div = divs.get(1);
					else
						div = divs.get(0);

					Elements links = div.getElementsByClass("img-link");
					for (Element link : links)
						if (link.tagName() == "a")
							registration.RegisterNewUrl(link.attr("abs:href"));
						

					Elements tmp = doc.select("a:contains(NEXT »)");
					
					if (tmp.isEmpty())
						break;
					else
						currentUrl = tmp.first().attr("abs:href");


				}
			}

			Thread.sleep(10000);
			registration.RegisterNewUrl("EXIT");
			registration.RegisterNewUrl("EXIT");
			System.out.println("Crawler worker finished working...");

		} catch (IOException e)
		{
			e.printStackTrace();
			System.out.println("Crawler worker finished working unexpectedly...");
			
		} catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Crawler worker finished working unexpectedly...");
		}
	}
}
